<?php
namespace Home\TagLib;
use Think\Template\TagLib;
defined('THINK_PATH') or exit();

class WHT extends TagLib{
	/**
	 * 定义标签列表
	 * @var array
	 */
	protected $tags   =  array(
			'position'   => array('attr' => 'pos,cate,limit,field,name,child', 'close' => 1),
			'list'       => array('attr' => 'name,category,child,row,field', 'close' => 1),
			'breadcrumb' => array('attr' => 'cate', 'close' => 0),
			'nav'        => array('close' => 0),
	);
	
	function _nav() {
		$navTree = M('Channel')->where('status=1')->order('sort asc')->select();;
		$navTree = list_to_tree($navTree);
		$parse  = '<ul class="clearfix" id="suckertree">';
		foreach ($navTree as $key=>$value) {
			$parse .= '<li><a href="'.U($value['url']).'">'.$value['title'].'</a>';
			if (isset($value['_child'])) {
				$parse .= '<ul>';
				foreach ($value['_child'] as $k=>$v) {
					$parse .= '<li><a href="'.U($v['url']).'">'.$v['title'].'</a></li>';
				}
				$parse .= '</ul>';
			}
			$parse .= '</li>';
		}
		$parse .= '</ul>';
		$parse .= '';
		return $parse;
	}
	
	function _breadcrumb($tag) {
		$cate = $tag['cate'];
		$parse  = '<?php ';
		$parse .= '$__BREAD__ = get_parent_category('.$cate.');';
		$parse .= ' ?>';
		$parse .= '<ul class="breadcrumb">';
		$parse .= '<li><a href="__ROOT__/">首页</a></li>';
		$parse .= '<volist name="__BREAD__" id="bread">';
		$parse .= '<li><a href="{:U("/lists/".$bread["name"])}">{$bread.title}</a></li>';
		$parse .= '</volist>';
		$parse .= '</ul>';
		return $parse;
	}
	
	public function _list($tag, $content){
		$name   = $tag['name'];
		$cate   = $tag['category'];
		$child  = empty($tag['child']) ? false : $tag['child'];
		$row    = empty($tag['row'])   ? '10' : $tag['row'];
		$field  = empty($tag['field']) ? 'true' : $tag['field'];
		if ($child) {
			$cate = D('Category')->getChild($cate);
			$cate = '\''.$cate.'\'';
		}
		$parse  = '<?php ';
		$parse .= '$__LIST__ = D(\'Document\')->limit('.$row.')->lists(';
		$parse .= $cate . ', \'`level` DESC,`id` DESC\', 1,';
		$parse .= $field . ');';
		$parse .= ' ?>';
		$parse .= '<volist name="__LIST__" id="'. $name .'">';
		$parse .= $content;
		$parse .= '</volist>';
		return $parse;
	}
	
	/* 推荐位列表 */
	public function _position($tag, $content){
		$pos    = $tag['pos'];
		$cate   = $tag['cate'];
		$name   = $tag['name'];
		$limit  = empty($tag['limit']) ? 'null' : $tag['limit'];
		$field  = empty($tag['field']) ? 'true' : $tag['field'];
		$child  = empty($tag['child']) ? false  : $tag['child'];
		if ($child && $cate != 'null') {
			$cate = D('Category')->getChild($cate);
	      	$cate = '\''.$cate.'\'';
		}
		$parse  = '<?php ';
		$parse .= '$__POSLIST__ = D(\'Document\')->position(';
		$parse .= $pos . ',';
		$parse .= $cate . ',\'';
		$parse .= $limit . '\',';
		$parse .= $field . ');';
		$parse .= ' ?>';
		$parse .= '<volist name="__POSLIST__" id="'. $name .'">';
		$parse .= $content;
		$parse .= '</volist>';
		return $parse;
	}
	
}